﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Collections;

namespace Common.Text
{
    public static class EncodingEx
    {
        public static readonly Dictionary<String, Encoding> s_Encodings;

        static EncodingEx()
        {
            EncodingEx.s_Encodings = new Dictionary<String, Encoding>();
            foreach (EncodingInfo enc in Encoding.GetEncodings())
            {
                Encoding encoding = enc.GetEncoding();
                String name = encoding.WebName.ToLower();
                if (!EncodingEx.s_Encodings.ContainsKey(name))
                {
                    EncodingEx.s_Encodings.Add(name, encoding);
                }
            }
        }

        public static Encoding FromWebName(String name)
        {
            Encoding retval;
            return EncodingEx.s_Encodings.TryGetValue(name.ToLower(), out retval) ? retval : null;
        }
    }
}
